package com.example.controller;

import com.example.impl.SolrSerivceImpl;
import com.example.impl.WxbGoodsServiceImpl;
import com.example.model.WxbGood;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private WxbGoodsServiceImpl wxbGoodsService;

    @Autowired
    private SolrSerivceImpl solrSerivce;


    @RequestMapping("/findAll")
    public String findAll(Model model){

       List<WxbGood> goodList = wxbGoodsService.findAll();
        model.addAttribute("goodList",goodList);

        return "goods-list";

    }



    @RequestMapping("/desc")
    public String desc(String goodId,Model model){

        WxbGood goods = wxbGoodsService.findById(goodId);
        model.addAttribute("goods",goods);

        return "goods-desc";
    }

    @RequestMapping("/search")
    @ResponseBody
    public SolrDocumentList search(String key){
        return  solrSerivce.listGoods(key);
    }

    @RequestMapping("/findById")
    public String findById(String goodId,Model model){
        WxbGood good = wxbGoodsService.findByAndId(goodId);
        model.addAttribute("good",good);
        return "order-details";
    }
}
